//
//  WeatherDetailsViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "WeatherDetailsViewController.h"
#import "WeatherCollectionViewCell.h"
#import "CoreDatamanager.h"
#import "Citys.h"
#import "WeatherAddViewController.h"
#import "WeatherViewController.h"
#import "WeatherModel.h"
@interface WeatherDetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout ,WeatherCollectionViewCellDelegate, WeatherAddViewControllerDelegate>


@property (nonatomic, retain) UICollectionView *collectionView;

//创建PageControl
@property (nonatomic, retain) UIPageControl *pageControl;
//头
@property (nonatomic, retain) UIView *titleView;
//头上的地名
@property (nonatomic, retain) UIButton *areaAdd;

//返回
@property (nonatomic, retain) UIButton *turnButton;
//头上的温度
@property (nonatomic, retain) UILabel *titleTem;

@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) UILabel *tmpLabel;

@property (nonatomic, retain) CoreDatamanager *coreDataManager;
@end

@implementation WeatherDetailsViewController

- (void)dealloc
{
    [_locationArea release];
    [_cityArray release];
    [_titleTem release];
    [_image release];
    [_tmpLabel release];
    [_coreDataManager release];
    [_collectionView release];
    [_titleView release];
    [_pageControl release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(NOtification:) name:@"push" object:nil];

}
- (void)NOtification:(NSNotification *)notification{
    
    WeatherViewController *weather = [[WeatherViewController alloc] init];
    weather.weather = notification.userInfo[@"loop"];
    if (weather.weather == nil) {
        return;
    }
    weather.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:weather animated:YES completion:nil];
    
}
- (void)viewWillAppear:(BOOL)animated{

    self.cityArray = [NSMutableArray array];

   self.coreDataManager = [CoreDatamanager shareManager];
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Citys"];
    NSArray *resultArray = [_coreDataManager.managedObjectContext executeFetchRequest:request error:nil];

    for (Citys *city in resultArray) {
       
        [self.cityArray addObject:city.cityName];
    }
    //创建背景图片
    [self createImageView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(weather1:) name:@"Image" object:nil];
    
    //创建头View
    [self createTitleView];
    
    //创建UICollectionView
    [self createCollectionView];
    
    //创建PageControl
    [self createPageControl];

    [request release];
    
}

#pragma mark 创建背景图片
- (void)createImageView{
    
    self.image = [[UIImageView alloc] init];
    self.image.frame = CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height);
    
    self.image.image = [UIImage imageNamed:@"wip_bk_sunny.jpg"];
    
    [self.view addSubview:self.image];
    
    [_image release];
}

#pragma mark 创建头
- (void)createTitleView{

    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375 *kWidth, 64 *kHeight)];
    self.titleView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:.3] ;
    [self.view addSubview:_titleView];
    self.areaAdd = [UIButton buttonWithType:UIButtonTypeCustom];
    self.areaAdd.frame = CGRectMake(80*kWidth, 14*kHeight, 215*kWidth, 35 *kHeight);
    [self.areaAdd addTarget:self action:@selector(AddAction) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"%ld", _index);
    [self.areaAdd setTitle:[NSString stringWithFormat:@"+%@",self.cityArray[self.index]] forState:UIControlStateNormal];
    self.areaAdd.titleLabel.font = [UIFont systemFontOfSize:17 *kWidth];
    [self.areaAdd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.turnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.turnButton.frame = CGRectMake(5*kWidth, 15 *kHeight, 40 *kWidth, 40 *kHeight);
    [self.turnButton setImage:[[UIImage imageNamed:@"ms_back@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.turnButton addTarget:self action:@selector(turn) forControlEvents:UIControlEventTouchUpInside];
    
    self.tmpLabel = [[UILabel alloc] init];
    
    _tmpLabel.frame = CGRectMake(-300 *kWidth, 15 *kWidth, 40 *kWidth, 40 *kHeight);

    [self.titleView addSubview:_tmpLabel];
    [_tmpLabel release];
    [_titleView addSubview:_turnButton];
    [_titleView addSubview:_areaAdd];
    [_titleView release];
    [_areaAdd release];
}
#pragma areaAdd的action
- (void)AddAction{

    WeatherAddViewController *add = [[WeatherAddViewController alloc] init];
    [self setHidesBottomBarWhenPushed:YES];
    add.delegate = self;
    [self.navigationController pushViewController:add animated:YES];    

}
- (void)weather1:(NSNotification *)notification{

    NSDictionary *dic = notification.userInfo[@"weather"];
    WeatherModel *weather = [WeatherModel weatherWithDictionary:dic];
    if ([[weather.now objectForKey:@"nowweather"] containsString:@"多云"]) {
        
        self.image.image = [UIImage imageNamed:@"duoyun.jpg"];
        
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"晴"]){
         self.image.image = [UIImage imageNamed:@"wip_bk_sunny.jpg"];
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"雨"]){
        self.image.image = [UIImage imageNamed:@"xiayu.jpg"];
    
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"雪"]){
        self.image.image = [UIImage imageNamed:@"xiaxue.jpg"];
    }else{
    
        self.image.image = [UIImage imageNamed:@"yintian.jpg"];
    
    }

}
#pragma mark WeatherAddViewController代理回调方法
- (void)send:(NSInteger)index{

    self.index = index;
}
#pragma mark 返回
- (void)turn{

    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 创建collectionView
- (void)createCollectionView{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.itemSize = CGSizeMake(self.view.frame.size.width ,(667 - 64) *kHeight);
    
    layout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 *kHeight , self.view.frame.size.width , (667 - 64) *kHeight) collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    self.collectionView.scrollEnabled = YES;
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.contentOffset = CGPointMake(self.index * self.view.frame.size.width, 0);
    
    [self.collectionView setBackgroundColor: [[UIColor whiteColor] colorWithAlphaComponent:0]];
    
    [self.collectionView registerClass:[WeatherCollectionViewCell class] forCellWithReuseIdentifier:@"WeatherCollectionViewCell"];
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [layout release];
}
#pragma mark 创建pageControl
- (void)createPageControl{

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(_areaAdd.frame.origin.x, _areaAdd.frame.origin.y + _areaAdd.frame.size.height - 2 *kHeight, _areaAdd.frame.size.width, 3 *kHeight)];
    [self.titleView addSubview:_pageControl];
   _pageControl.numberOfPages = self.cityArray.count;
    _pageControl.currentPage = _index;
    [_pageControl release];

}

#pragma mark collectionView代理回调方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _cityArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WeatherCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WeatherCollectionViewCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.block = ^(CGFloat change,NSString *tmp){
        
        if (change >= 250 && change <= 340) {
             self.turnButton.frame = CGRectMake(5 *kWidth - (change - 250) *kWidth, 15 *kHeight, 40 *kWidth, 40 *kHeight);
        }
        if ( change <= 340){
            
            self.tmpLabel.frame = CGRectMake(- 320 *kWidth + change *kWidth, 10 *kWidth, 50 *kWidth, 60 *kHeight);
            self.tmpLabel.text = [NSString stringWithFormat:@"%@℃",tmp];
            self.tmpLabel.font = [UIFont systemFontOfSize:19 *kWidth];
            self.tmpLabel.textColor = [UIColor whiteColor];
        
        }
        self.titleView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:change / 1000 + 0.2];
        self.collectionView.backgroundColor = [[UIColor darkGrayColor]colorWithAlphaComponent:change / 1000 + 0.2];
    };
    cell.url = _cityArray[indexPath.row];
    return cell;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    

    int i = self.collectionView.contentOffset.x / self.view.frame.size.width;
    [self.areaAdd setTitle:[NSString stringWithFormat:@"+%@",self.cityArray[i]] forState:UIControlStateNormal];
    
    self.pageControl.currentPage = i;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
